package fr.cephb.joperon.structures;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import fr.cephb.util.JSONBuilder;
import fr.cephb.util.JSONizable;

/**

Defined by Mario as

<pre>typedef struct stypes_D
{
    u_int64_t             a:8;
    u_int64_t             c:8;
    u_int64_t             g:8;
    u_int64_t             t:8;
    u_int64_t             n:8;
    u_int64_t             d:8;
    u_int64_t             i:8;
    u_int64_t             f:8;
} TYPE_T;</pre>


 */
public class STypes
	implements Serializable,JSONizable
	{
	private static final long serialVersionUID = 1L;
	public int a;
	public int c;
	public int g;
	public int t;
	public int n;
	public int d;
	public int i;
	public int f;
	
	/** in the context of a reference Stype, answer if this position
	 * is part of a repeat
	 * @return
	 */
	public boolean isRepeat()
		{
		return f==3 || f==1;
		}
	
	/** in the context of a reference Stype, answer if this position
	 * is part a known SNP
	 * @return
	 */
	public boolean isSnp()
		{
		return f==3 || f==2;
		}
	
	public char getMajorBase()
		{
		char base='N';
		int n=0;
		if(a>n) { n=a; base='A';}
		if(t>n) { n=t; base='T';}
		if(c>n) { n=c; base='C';}
		if(g>n) { n=g; base='G';}
		return base;
		}

	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		result = prime * result + a;
		result = prime * result + c;
		result = prime * result + d;
		result = prime * result + f;
		result = prime * result + g;
		result = prime * result + i;
		result = prime * result + n;
		result = prime * result + t;
		return result;
		}

	@Override
	public boolean equals(Object obj)
		{
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		STypes other = (STypes) obj;
		if (a != other.a) return false;
		if (c != other.c) return false;
		if (d != other.d) return false;
		if (f != other.f) return false;
		if (g != other.g) return false;
		if (i != other.i) return false;
		if (n != other.n) return false;
		if (t != other.t) return false;
		return true;
		}
	/**
	 * returns an associative array from letter to String
	 * @return
	 */
	public Map<String,Integer> toMap()
		{
		Map<String,Integer> m=new HashMap<String, Integer>(8);
		m.put("A", a);
		m.put("C", c);
		m.put("G", g);
		m.put("G", t);
		m.put("N", n);
		m.put("D", d);
		m.put("I", i);
		m.put("F", f);
		return m;
		}
	
	public int getA()
		{
		return a;
		}
	
	public int getC()
		{
		return c;
		}
	public int getG()
		{
		return g;
		}
	public int getT()
		{
		return t;
		}
	public int getN()
		{
		return n;
		}
	public int getD()
		{
		return d;
		}
	
	public int getI()
		{
		return i;
		}
	
	public int getF()
		{
		return f;
		}
	
	@Override
	public String toJSON()
		{
		return new JSONBuilder()
				.put("A", a)
				.put("C", c)
				.put("G", g)
				.put("G", t)
				.put("N", n)
				.put("D", d)
				.put("I", i)
				.put("F", f)
				.toString();
		}
	
	@Override
	public String toString()
		{
		return toJSON();
		}
	
	}
